package com.hugothauan.cowboyshoot;

import java.util.ArrayList;
import java.util.List;

import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.hugothauan.cowboyshoot.*;
import com.game.ThauanFramework.BitmapStorage;
import com.game.ThauanFramework.GameObject;
import com.game.ThauanFramework.SaveManager;
import com.game.ThauanFramework.TimeManager;

public class SplashView extends View implements Runnable {

	private float splashTime = 3;
	private final String TAG = "Splash View";

	private Thread mainThread;
	private int interval = 10;

	private boolean BitmapStorageLauched = false;
	private boolean running = true;

	private List<GameObject> gameObjects = new ArrayList<GameObject>();

	public SplashView(Context context) {
		super(context);

		running = true;
		this.setFocusable(true);

		Start();
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		if (MinhasCoisas.getCanvasHeight() == 0) {
			MinhasCoisas.setCanvasHeight(h);
			MinhasCoisas.setCanvasWidth(w);
		}

		this.setBackgroundResource(R.drawable.splash_hugo_thauan);

		mainThread = new Thread(this);
		mainThread.start();

		Log.i(TAG, "on size Changed");
	}

	public boolean onTouchEvent(MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			int x = (int) event.getRawX();
			int y = (int) event.getRawY();

			break;
		}
		return super.onTouchEvent(event);
	}

	// ------------------------------------------------------------- START
	public void Start() {
		
	}
	// ------------------------------------------------------------- UPDATE

	float cronometro = 0;

	public void Update() {
		if (cronometro < this.splashTime) {
			cronometro += TimeManager.getInstance().getDeltaTime();

			Log.i(TAG, String.valueOf(cronometro));

			if (cronometro >= 2 && !BitmapStorageLauched) {
				BitmapStorageLauched = true;
				BitmapStorage.getInstance();
			}
		} else {

			this.SetAlive(false);
			
			// Go to the menu Activity
			Intent menuScene = new Intent(MinhasCoisas.getCurrentActivity(),
					Menu.class);
			MinhasCoisas.getCurrentActivity().startActivity(menuScene);
			Log.i(TAG, "Fim de Splash");

		}

	}

	// ------------------------------------------------------------- DRAW
	@Override
	public void draw(Canvas canvas) {
		super.draw(canvas);
		for (GameObject go : gameObjects) {
			go.Draw(canvas);
		}

	}

	public void SetAlive(boolean bool) {
		this.running = bool;
	}

	@Override
	public void run() {

		// Starts the loop
		while (running) {
			try {
				Thread.sleep(interval);
			} catch (InterruptedException e) {
				running = false;
				Log.i(TAG, "Erro no processo de Thread", e);
			}

			Update();
			postInvalidate();
		}

	}
}
